define(function(require, exports, module)
{
  var $ = require("$");
  var bootbox = require("bootbox");
  var dtUtil = require('datatableutil');
  var loader = require("./loader/loader");
  require("bootstrap_commonrequire");
  require("moment");
  require('daterangepicker.css');
  require('bootstrap-daterangepicker');
  require('select2.css');
  require('select2');
    
    exports.init = function(param)
    {
        //加载表单模板和数据
        var Table = require("table");
        // 查询内容显示
        new Table(
                {
                    attrs :
                    {
                        id : "data_table",
                        isHovered : false,
                        isStriped : true,
                        isBordered : false
                    },
                    renderTo : param.divWrapperId
                   
                });
        var accountinfo_data_table = $('#data_table').dataTable({
            "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
            "bJQueryUI" : false,
            "oLanguage" : {
              "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
            },
            "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
            'bPaginate' : true, // 是否分页
            "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
            "bServerSide" : true, // 指定服务器端分页
            "bDeferRender" : true,
            "bFilter" : true,
            "bSort" : true,
            "bInfo" : true,
            "sAjaxSource" : param.ajaxUrl,
            "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.DEFAULT, // 默认分页的每页记录数
            "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
              var queryparam = dtUtil.convert_aoData(aoData);
              // 添加自己的额外参数
              oSettings.jqXHR = $.ajax({
                "dataType" : 'json',
                "contentType" : "application/json;charset=UTF-8",
                "type" : "GET",
                "url" : sSource,
                "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
                "success" : function(json) {
                  if (json.flag == '000000') {
                    // 隐藏蒙版
                    loader.noloading();
                    dtUtil.successCallback(json, aoData, fnCallback);
                  } else {
                    oauth2Relogin();
                  }
                }
              });

            },
            "aoColumns" : [ {
              "sTitle" : "id",
              "bSortable" : false,
              "mData" : "id",
              "bVisible" : false
            }, {
              "sTitle" : "信控产品名称",
              "bSortable" : false,
              "mData" : "ability_type",
              "bVisible" : true,
              "mRender" : function(data, type, full) {
                  if (data === "VoiceNotice3")
                    return "IMS文字语音通知";
                  else if (data === "AudeoSend")
                    return "IMS录音语音通知";
                  else if (data === "RongLianVoice")
                	  return "容联文字语音通知";
                  else if (data === "RongLianAudeo")
                	  return "容联录音语音通知"; 
                  else if (data === "TianYiVoice")
                	  return "天亿文字语音通知";
                  else if (data === "TianYiAudeo")
                	  return "天亿录音语音通知"; 
                  else
                    return "未知";
               }
            }, {
              "sTitle" : "应用标识",
              "bSortable" : false,
              "mData" : "app_key",
              "bVisible" : true
//              "mRender" : function(data, type, full) {
//                  if (data === "-1")
//                    return "全部应用";
//                  else
//                    return data;
//               }
            }, {
              "sTitle" : "发送数量限制",
              "bSortable" : false,
              "mData" : "send_count"
            }, {
              "sTitle" : "报备号码类型",
              "bSortable" : false,
              "mData" : "displaynum_record",
              "bVisible" : true,
              "mRender" : function(data, type, full) {
                  if (data === "yes")
                    return "有报备";
                  else if (data === "no")
                    return "无报备";
                  else
                    return "全部";
               }
            }, {
              "sTitle" : "状态",
              "bSortable" : false,
              "mData" : "use_flag",
              "bVisible" : true,
              "mRender" : function(data, type, full) {
                if (data === "yes")
                  return "启用";
                else if(data === "no")
                  return "停用";
                else
                  return "未知";
             }
            }, {
                "sTitle" : "操作",
                "bSortable" : false,
                "mData" : null,
                "mRender" : function(data, status, full)
                {
                  var ret = '<div class="btn-group" style="margin-top: 8px;">';
                      ret += '<a href="javascript:void(0);" class="btn icon-reply contrast" title="修改"></a>';
                      ret += '<a href="javascript:void(0);" class="btn icon-rocket contrast" title="启用/停用"></a>';
                  return ret += '</div>';
                }
              }],
            "fnDrawCallback" : function(oSettings) {
              // 表格绘制完成后，去除width属性，使表格可缩放
              $('#' + oSettings.sTableId).css("width", "");
            }
          });
            
           /**
            * 新建信控规则
            */
            $("#creatRule").on("click", function()
            {
              $("#accountForm").remove();
              var appForm = require("./infocontrolform");
              appForm.init(param.divWrapperId, null); 
            });
            /**
             * 修改信控规则
             */
            $("#data_table tbody").on("click", "tr>td>div>a.icon-reply", function(e)
            {
              $("#accountForm").remove();
              var rowNode = this.parentNode.parentNode.parentNode;
              var rowData = accountinfo_data_table.fnGetData(rowNode);
              var infocontrolupdateform = require("./infocontrolupdateform");
              infocontrolupdateform.init(param.divWrapperId, rowData);
            }); 
          //启用/停用
            $("#data_table tbody").on("click", "tr>td>div>a.icon-rocket", function(e)
            {
              var rowNode = this.parentNode.parentNode.parentNode;
              var rowData= accountinfo_data_table.fnGetData(rowNode);
              loader.loading("正在提交，请稍等……");
              var use_flag=rowData.use_flag;
              use_flag=(use_flag==='yes')?'no':'yes';
              $.ajax({
                url : "/infocontrol/updateInfoControlRuleStatus.do",
                dataType : "json",
                data : "data=" + JSON.stringify({
                	id:rowData.id,
                	useFlag:use_flag
                }),
                type : "POST",
                success : function(json) {
                  if(json.flag == '000000'){
                    $("#data_table").dataTable().fnFilter(); 
                    loader.noloading();                   
                  } else {
                    oauth2Relogin();                    
                  }
                },
                error : function(s) {
                  loader.noloading();
                  bootbox.alertTimeout("网络请求失败，请重试！");
                }
              });
            });         
    };
});

